package com.xyht.sca_s.student_manage_system.modules.assetConsumableBorrow.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ExceptionCast;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.common.util.CacheUtil;
import com.xyht.sca_s.student_manage_system.modules.asset.entity.SmsAssetCategoryManagement;
import com.xyht.sca_s.student_manage_system.modules.assetConsumable.entity.SmsAssetConsumableCategoryManagement;
import com.xyht.sca_s.student_manage_system.modules.assetConsumable.entity.SmsAssetConsumableStorage;
import com.xyht.sca_s.student_manage_system.modules.assetConsumable.mapper.SmsAssetConsumableCategoryManagementMapper;
import com.xyht.sca_s.student_manage_system.modules.assetConsumable.mapper.SmsAssetConsumableStorageMapper;
import com.xyht.sca_s.student_manage_system.modules.asset.entity.SmsAssetProcess;
import com.xyht.sca_s.student_manage_system.modules.asset.entity.SmsAssetProcessDetail;
import com.xyht.sca_s.student_manage_system.modules.asset.mapper.SmsAssetCategoryManagementMapper;

import com.xyht.sca_s.student_manage_system.modules.asset.mapper.SmsAssetProcessDetailMapper;
import com.xyht.sca_s.student_manage_system.modules.asset.mapper.SmsAssetProcessMapper;
import com.xyht.sca_s.student_manage_system.modules.assetConsumableBorrow.entity.SmsAssetConsumableBorrowApply;
import com.xyht.sca_s.student_manage_system.modules.assetConsumableBorrow.entity.req.SmsAssetConsumableBorrowApplyAllowReq;
import com.xyht.sca_s.student_manage_system.modules.assetConsumableBorrow.entity.req.SmsAssetConsumableBorrowApplyGoodsReq;
import com.xyht.sca_s.student_manage_system.modules.assetConsumableBorrow.entity.req.SmsAssetConsumableBorrowApplyProcessDetailReq;
import com.xyht.sca_s.student_manage_system.modules.assetConsumableBorrow.entity.req.SmsAssetConsumableBorrowApplyReq;
import com.xyht.sca_s.student_manage_system.modules.assetConsumableBorrow.entity.resp.SmsAssetConsumableBorrowApplyGoodsResp;
import com.xyht.sca_s.student_manage_system.modules.assetConsumableBorrow.entity.resp.SmsAssetConsumableBorrowApplyMPJResp;
import com.xyht.sca_s.student_manage_system.modules.assetConsumableBorrow.entity.resp.SmsAssetConsumableBorrowApplyProcessDetailResp;
import com.xyht.sca_s.student_manage_system.modules.assetConsumableBorrow.entity.resp.SmsAssetConsumableBorrowApplyResp;
import com.xyht.sca_s.student_manage_system.modules.assetConsumableBorrow.mapper.SmsAssetConsumableBorrowApplyMapper;
import com.xyht.sca_s.student_manage_system.modules.assetConsumableBorrow.service.SmsAssetConsumableBorrowApplyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xyht.sca_s.student_manage_system.modules.assetConsumableBorrow.entity.*;

import com.xyht.sca_s.student_manage_system.modules.assetConsumableBorrow.mapper.*;
import com.xyht.sca_s.student_manage_system.modules.newTips.myEnum.AppTypeEnum;
import com.xyht.sca_s.student_manage_system.modules.newTips.myEnum.RangeTypeEnum;
import com.xyht.sca_s.student_manage_system.modules.newTips.myEnum.TipsTypeEnum;
import com.xyht.sca_s.student_manage_system.modules.newTips.util.NewTipsUtil;
import com.xyht.sca_s.student_manage_system.modules.user.entity.SmsRole;
import com.xyht.sca_s.student_manage_system.modules.user.entity.SmsUser;
import com.xyht.sca_s.student_manage_system.modules.user.entity.resp.SmsSearchUserResp;
import com.xyht.sca_s.student_manage_system.modules.user.mapper.SmsRoleMapper;
import com.xyht.sca_s.student_manage_system.modules.user.mapper.SmsUserMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import static com.xyht.sca_s.student_manage_system.common.util.ParamCheckUtil.isNullOrEmpty;
import static com.xyht.sca_s.student_manage_system.modules.asset.constant.AssetAllowStatusConstant.ASSET_ALLOW_STATUS_PASS;
import static com.xyht.sca_s.student_manage_system.modules.asset.constant.AssetBooleanConstant.ASSET_BOOLEAN_FALSE;
import static com.xyht.sca_s.student_manage_system.modules.asset.constant.AssetBooleanConstant.ASSET_BOOLEAN_TRUE;
import static com.xyht.sca_s.student_manage_system.modules.asset.constant.AssetObjectTypeConstant.ASSET_OBJECT_TYPE_PERSON;
import static com.xyht.sca_s.student_manage_system.modules.asset.constant.ProcessObjectTypeConstant.PROCESS_OBJECT_TYPE_PERSON;
import static com.xyht.sca_s.student_manage_system.modules.assetConsumableBorrow.constant.AssetAllowStatusConstant.*;
import static com.xyht.sca_s.student_manage_system.modules.assetConsumableBorrow.constant.AssetAllowStatusConstant.ASSET_BORROW_ALLOW_STATUS_CANCEL;
import static com.xyht.sca_s.student_manage_system.modules.assetConsumableBorrow.constant.AssetApplyTypeConstant.*;
import static com.xyht.sca_s.student_manage_system.modules.assetConsumableBorrow.constant.AssetBooleanConstant.ASSET_BORROW_BOOLEAN_TRUE;
import static com.xyht.sca_s.student_manage_system.modules.assetConsumableBorrow.constant.AssetObjectTypeConstant.ASSET_BORROW_OBJECT_TYPE_PERSON;
import static com.xyht.sca_s.student_manage_system.modules.assetConsumableBorrow.constant.AssetObjectTypeConstant.ASSET_BORROW_OBJECT_TYPE_ROLE;
import static com.xyht.sca_s.student_manage_system.modules.assetConsumableBorrow.constant.AssetProcessTypeConstant.ASSET_PROCESS_TYPE_CONSUMABLE_GET_STORAGE;

/**
 * <p>
 * 低值易耗品领用申请 服务实现类
 * </p>
 *
 * @author XYHT
 * @since 2023-06-30
 */
@Service
public class SmsAssetConsumableBorrowApplyServiceImpl extends ServiceImpl<SmsAssetConsumableBorrowApplyMapper, SmsAssetConsumableBorrowApply> implements SmsAssetConsumableBorrowApplyService {
    @Autowired
    SmsAssetConsumableBorrowApplyMapper smsAssetConsumableBorrowApplyMapper;
    @Autowired
    SmsAssetProcessDetailMapper smsAssetProcessDetailMapper;
    @Resource
    private SmsAssetConsumableCategoryManagementMapper smsAssetCategoryManagementMapper;
    @Autowired
    SmsAssetConsumableBorrowApplyGoodsMapper smsAssetConsumableBorrowApplyGoodsMapper;
    @Autowired
    SmsAssetProcessMapper smsAssetProcessMapper;
    @Autowired
    SmsAssetConsumableBorrowApplyProcessDetailMapper smsAssetConsumableBorrowApplyProcessDetailMapper;
    @Autowired
    NewTipsUtil newTipsUtil;
    @Autowired
    CacheUtil cacheUtil;
    @Autowired
    smsAssetConsumableBorrowApplyProcessDetailMPJMapper smsAssetConsumableBorrowApplyProcessDetailMPJMapper;
    @Autowired
    SmsAssetConsumableStorageMapper smsAssetConsumableStorageMapper;
    @Autowired
    private SmsRoleMapper smsRoleMapper;
    @Resource
    private SmsUserMapper smsUserMapper;

    @Override
    @Transactional
    public ResponseResult smsAssetConsumableBorrowApplyAdd(SmsAssetConsumableBorrowApplyReq applyReq, String userId) {
        if (isNullOrEmpty(applyReq.getApplyDepartment()) || isNullOrEmpty(applyReq.getGoodsList())
                || isNullOrEmpty(applyReq.getApplyTime()) || isNullOrEmpty(applyReq.getApplyUse())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        if (applyReq.getGoodsList().size() < 1) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        SmsAssetProcess originProcess1 = smsAssetProcessMapper.selectOne(new QueryWrapper<SmsAssetProcess>()
                .lambda()
                .eq(SmsAssetProcess::getProcessType, ASSET_PROCESS_TYPE_CONSUMABLE_GET_STORAGE));
        if (originProcess1 == null) {
            ExceptionCast.cast(CommonResult.failed(CommonCodeEnum.ASSET_PROCESS_NOT_SETTING));
        }
        List<SmsAssetProcessDetail> fillProcessList =
                smsAssetProcessDetailMapper.selectList(new QueryWrapper<SmsAssetProcessDetail>()
                        .lambda()
                        .eq(SmsAssetProcessDetail::getProcessId, originProcess1.getId())
                        .eq(SmsAssetProcessDetail::getProcessDetailIsApplicantEdit, ASSET_BOOLEAN_TRUE)
                        .eq(SmsAssetProcessDetail::getProcessDetailIsUse, ASSET_BOOLEAN_TRUE)
                        .orderByAsc(SmsAssetProcessDetail::getProcessDetailSort));
        if (fillProcessList != null && fillProcessList.size() > 0) {
            if (applyReq.getProcessList() == null || applyReq.getProcessList().size() < 1) {
                return CommonResult.failed(CommonCodeEnum.ASSET_BORROW_ALLOW_USER_NOT_SETTING);
            }
            //判断申请人是否将全部需要编辑流程填写
            List<SmsAssetProcessDetail> filterList = fillProcessList
                    .stream()
                    .filter(process -> {
                                List<SmsAssetConsumableBorrowApplyProcessDetailReq> result = applyReq.getProcessList()
                                        .stream()
                                        .filter(fillProcess -> fillProcess.getProcessDetailId().equals(process.getId()))
                                        .collect(Collectors.toList());
                                //匹配的去除  未匹配的留下
                                if (result.size() > 0) {
                                    return false;
                                } else {
                                    return true;
                                }
                            }
                    )
                    .collect(Collectors.toList());
            if (filterList.size() > 0) {
                return CommonResult.failed(CommonCodeEnum.ASSET_BORROW_ALLOW_PROCESS_NOT_FILL);
            }
        }

        //创建申请表
        SmsAssetConsumableBorrowApply smsAssetConsumableBorrowApply = new SmsAssetConsumableBorrowApply();
        BeanUtils.copyProperties(applyReq, smsAssetConsumableBorrowApply);
        smsAssetConsumableBorrowApply.setApplyUserId(userId);
        smsAssetConsumableBorrowApply.setAllowStatus(ASSET_BORROW_ALLOW_STATUS_WAIT_ALLOW);
        smsAssetConsumableBorrowApplyMapper.insert(smsAssetConsumableBorrowApply);
        if (isNullOrEmpty(smsAssetConsumableBorrowApply.getId())) {
            ExceptionCast.cast(CommonResult.failed(CommonCodeEnum.FAIL));
        }
        String applyId = smsAssetConsumableBorrowApply.getId();
        //创建借用申请物品记录
        for (SmsAssetConsumableBorrowApplyGoodsReq goodsReq : applyReq.getGoodsList()) {
            //校验物品是否正常传入
            if (isNullOrEmpty(goodsReq.getId(),goodsReq.getAssetUnit()) || isNullOrEmpty(goodsReq.getAssetCategory())
                    || isNullOrEmpty(goodsReq.getAssetModel()) || isNullOrEmpty(goodsReq.getAssetName())
                    || isNullOrEmpty(goodsReq.getAssetShouldNum()) || isNullOrEmpty(goodsReq.getAssetPrice())) {
                ExceptionCast.cast(CommonResult.failed(CommonCodeEnum.INVALID_PARAM));
            }
            SmsAssetConsumableCategoryManagement smsAssetCategoryManagement =
                    smsAssetCategoryManagementMapper.selectById(goodsReq.getAssetCategory());
            if (isNullOrEmpty(smsAssetCategoryManagement.getId())) {
                ExceptionCast.cast(CommonResult.failed(CommonCodeEnum.ASSET_BORROW_CATEGORY_NOT_EXIST));
            }
            //判断是否为根类别
            if (smsAssetCategoryManagement.getId().length() < 3) {
                ExceptionCast.cast(CommonResult.failed(CommonCodeEnum.ASSET_BORROW_CATEGORY_CAN_NOT_USE_ROOT));
            }

            //判断数量
            if (goodsReq.getAssetShouldNum() < 0) {
                ExceptionCast.cast(CommonResult.failed(CommonCodeEnum.ASSET_BORROW_STORAGE_NUM_ERR));
            }
            //校验物品
            SmsAssetConsumableStorage smsAssetConsumableStorage = smsAssetConsumableStorageMapper.selectById(goodsReq.getId());
            if (isNullOrEmpty(smsAssetConsumableStorage)) {
                return CommonResult.failed(CommonCodeEnum.CONSUMABLE_NOT_EXIST);
            }
            SmsAssetConsumableBorrowApplyGoods smsAssetConsumableBorrowApplyGoods = new SmsAssetConsumableBorrowApplyGoods();
            BeanUtils.copyProperties(goodsReq, smsAssetConsumableBorrowApplyGoods,"id");
            smsAssetConsumableBorrowApplyGoods.setApplyId(applyId);
            smsAssetConsumableBorrowApplyGoods.setAssetConsumableId(goodsReq.getId());
            //计算总金额
            BigDecimal totalPrice = goodsReq.getAssetPrice().multiply(new BigDecimal(goodsReq.getAssetShouldNum()));
            smsAssetConsumableBorrowApplyGoods.setTotalPrice(totalPrice);
            smsAssetConsumableBorrowApplyGoodsMapper.insert(smsAssetConsumableBorrowApplyGoods);
        }
        //创建审批流程
        //主流程

        //流程节点
        List<SmsAssetProcessDetail> originProcessDetailList =
                smsAssetProcessDetailMapper.selectList(new QueryWrapper<SmsAssetProcessDetail>()
                        .lambda()
                        .eq(SmsAssetProcessDetail::getProcessDetailIsUse, ASSET_BOOLEAN_TRUE)
                        .eq(SmsAssetProcessDetail::getProcessId, originProcess1.getId())
                        .orderByAsc(SmsAssetProcessDetail::getProcessDetailSort));
        if (originProcessDetailList == null || originProcessDetailList.size() < 1) {
            ExceptionCast.cast(CommonResult.failed(CommonCodeEnum.ASSET_BORROW_PROCESS_NOT_SETTING));
        }

        int sort = 1;
        for (SmsAssetProcessDetail process : originProcessDetailList) {
            //判断管理员是否配置初始流程
            if (process.getProcessDetailIsApplicantEdit().equals(ASSET_BOOLEAN_FALSE)) {
                if (isNullOrEmpty(process.getProcessDetailObjectId()) || isNullOrEmpty(process.getProcessDetailObjectType())) {
                    ExceptionCast.cast(CommonResult.failed(CommonCodeEnum.PAYMENT_PROCESS_NOT_SETTING));
                }
            }

            SmsAssetConsumableBorrowApplyProcessDetail addDetail =
                    new SmsAssetConsumableBorrowApplyProcessDetail();

            addDetail.setApplyId(applyId);
            addDetail.setProcessDetailId(process.getId());
            addDetail.setProcessDetailIsSign(process.getProcessDetailIsSign());
            addDetail.setProcessDetailIsStamp(process.getProcessDetailIsStamp());
            addDetail.setProcessDetailName(process.getProcessDetailName());
            addDetail.setProcessDetailStatus(ASSET_BORROW_ALLOW_STATUS_WAIT_ALLOW);

            addDetail.setProcessDetailSort(sort++);

            //申请人可编辑流程
            if (process.getProcessDetailIsApplicantEdit().equals(ASSET_BOOLEAN_TRUE)) {
                List<SmsAssetConsumableBorrowApplyProcessDetailReq> filterList = applyReq.getProcessList()
                        .stream()
                        .filter(fillProcess -> fillProcess.getProcessDetailId().equals(process.getId()))
                        .collect(Collectors.toList());
                if (filterList.size() < 1) {
                    ExceptionCast.cast(CommonResult.failed(CommonCodeEnum.PAYMENT_ALLOW_PROCESS_NOT_FILL));
                }
                SmsAssetConsumableBorrowApplyProcessDetailReq fillProcessReq = filterList.get(0);

                if (isNullOrEmpty(fillProcessReq.getProcessDetailObjectId())) {
                    ExceptionCast.cast(CommonResult.failed(CommonCodeEnum.INVALID_PARAM));
                }
                addDetail.setProcessDetailObjectId(fillProcessReq.getProcessDetailObjectId());
                addDetail.setProcessDetailObjectType(fillProcessReq.getProcessDetailObjectType());
            } else {
                addDetail.setProcessDetailObjectId(process.getProcessDetailObjectId());
                addDetail.setProcessDetailObjectType(process.getProcessDetailObjectType());
            }
            smsAssetConsumableBorrowApplyProcessDetailMapper.insert(addDetail);
        }


        //消息通知
        //通知流程第一个节点
        SmsAssetConsumableBorrowApplyProcessDetail firstDetail =
                smsAssetConsumableBorrowApplyProcessDetailMapper.selectOne(new QueryWrapper<SmsAssetConsumableBorrowApplyProcessDetail>()
                        .lambda()
                        .eq(SmsAssetConsumableBorrowApplyProcessDetail::getApplyId, applyId)
                        .eq(SmsAssetConsumableBorrowApplyProcessDetail::getProcessDetailSort, 1)
                        .orderByAsc(SmsAssetConsumableBorrowApplyProcessDetail::getProcessDetailSort));
        RangeTypeEnum rangeTypeEnum;
        if (firstDetail.getProcessDetailIsApplicantEdit().equals(1)) {
            rangeTypeEnum = RangeTypeEnum.RANGE_TYPE_ENUM_PERSON;
        } else {
            if (firstDetail.getProcessDetailObjectType() == ASSET_BORROW_OBJECT_TYPE_PERSON) {
                rangeTypeEnum = RangeTypeEnum.RANGE_TYPE_ENUM_PERSON;
            } else {
                rangeTypeEnum = RangeTypeEnum.RANGE_TYPE_ENUM_ROLE;
            }
        }

        List<String> rangeList = new ArrayList<>();
        rangeList.add(firstDetail.getProcessDetailObjectId());

        newTipsUtil.addNewTips(TipsTypeEnum.TIPS_TYPE_ENUM_ALLOW,
                userId,
                rangeTypeEnum,
                rangeList,
                AppTypeEnum.APP_TYPE_ENUM_ASSET_CONSUMABLE_ACCEPTANCE,
                applyId,
                firstDetail.getId());
        return CommonResult.success();
    }

    @Override
    @Transactional
    public ResponseResult smsAssetConsumableBorrowApplyCancel(SmsAssetConsumableBorrowApplyReq cancelReq, String userId) {
        if (isNullOrEmpty(cancelReq.getApplyId())) {
            ExceptionCast.cast(CommonResult.failed(CommonCodeEnum.INVALID_PARAM));
        }
        SmsAssetConsumableBorrowApply assetBorrowApply = smsAssetConsumableBorrowApplyMapper.selectOne(new QueryWrapper<SmsAssetConsumableBorrowApply>()
                .lambda()
                .eq(SmsAssetConsumableBorrowApply::getApplyUserId, userId)
                .eq(SmsAssetConsumableBorrowApply::getId, cancelReq.getApplyId()));
        if (assetBorrowApply == null) {
            ExceptionCast.cast(CommonResult.failed(CommonCodeEnum.ASSET_BORROW_APPLY_NOT_EXIST));
        }
        if (assetBorrowApply.getAllowStatus() != ASSET_BORROW_ALLOW_STATUS_WAIT_ALLOW) {
            ExceptionCast.cast(CommonResult.failed(CommonCodeEnum.ASSET_BORROW_CANCEL_FAIL_ALLOWED));
        }
        //判断是否审批中
        List<SmsAssetConsumableBorrowApplyProcessDetail> processDetailList =
                smsAssetConsumableBorrowApplyProcessDetailMapper.selectList(new QueryWrapper<SmsAssetConsumableBorrowApplyProcessDetail>()
                        .lambda()
                        .eq(SmsAssetConsumableBorrowApplyProcessDetail::getApplyId, cancelReq.getApplyId())
                        .eq(SmsAssetConsumableBorrowApplyProcessDetail::getProcessDetailStatus, ASSET_BORROW_ALLOW_STATUS_PASS));
        if (processDetailList.size() > 0 && processDetailList != null) {
            ExceptionCast.cast(CommonResult.failed(CommonCodeEnum.ASSET_BORROW_CANCEL_FAIL_ALLOWED));
        }
        //撤销申请
        SmsAssetConsumableBorrowApply cancelApply = new SmsAssetConsumableBorrowApply();
        cancelApply.setId(cancelReq.getApplyId());
        cancelApply.setAllowStatus(ASSET_BORROW_ALLOW_STATUS_CANCEL);
        cancelApply.setEndTime(new Date());
        smsAssetConsumableBorrowApplyMapper.updateById(cancelApply);

        //修改节点状态
        SmsAssetConsumableBorrowApplyProcessDetail cancelProcessDetail = new SmsAssetConsumableBorrowApplyProcessDetail();
        cancelProcessDetail.setProcessDetailStatus(ASSET_BORROW_ALLOW_STATUS_CANCEL);
        smsAssetConsumableBorrowApplyProcessDetailMapper.update(cancelProcessDetail, new QueryWrapper<SmsAssetConsumableBorrowApplyProcessDetail>()
                .lambda()
                .eq(SmsAssetConsumableBorrowApplyProcessDetail::getApplyId, cancelReq.getApplyId()));
        //消息通知
        newTipsUtil.cancelFinishTips(cancelReq.getApplyId());
        return CommonResult.success();
    }

    @Override
    @Transactional
    public ResponseResult smsAssetConsumableBorrowApplyAllow(SmsAssetConsumableBorrowApplyAllowReq allowReq, String userId) {
        if (isNullOrEmpty(allowReq.getApplyId()) || isNullOrEmpty(allowReq.getDetailId())
                || isNullOrEmpty(allowReq.getProcessDetailStatus())) {
            ExceptionCast.cast(CommonResult.failed(CommonCodeEnum.INVALID_PARAM));
        }
        if (allowReq.getProcessDetailStatus() != ASSET_BORROW_ALLOW_STATUS_PASS
                && allowReq.getProcessDetailStatus() != ASSET_BORROW_ALLOW_STATUS_DENY) {
            ExceptionCast.cast(CommonResult.failed(CommonCodeEnum.INVALID_PARAM));
        }
        SmsAssetConsumableBorrowApply allowApply = smsAssetConsumableBorrowApplyMapper.selectById(allowReq.getApplyId());
        if (isNullOrEmpty(allowApply)) {
            ExceptionCast.cast(CommonResult.failed(CommonCodeEnum.ASSET_BORROW_APPLY_NOT_EXIST));
        }
        SmsAssetConsumableBorrowApplyProcessDetail processDetail =
                smsAssetConsumableBorrowApplyProcessDetailMapper.selectOne(new QueryWrapper<SmsAssetConsumableBorrowApplyProcessDetail>()
                        .lambda()
                        .eq(SmsAssetConsumableBorrowApplyProcessDetail::getApplyId, allowReq.getApplyId())
                        .eq(SmsAssetConsumableBorrowApplyProcessDetail::getId, allowReq.getDetailId())
                        .eq(SmsAssetConsumableBorrowApplyProcessDetail::getProcessDetailStatus, ASSET_BORROW_ALLOW_STATUS_WAIT_ALLOW));
        if (isNullOrEmpty(processDetail.getId())) {
            ExceptionCast.cast(CommonResult.failed(CommonCodeEnum.ASSET_ALLOW_FAIL));
        }

        //判断该节点是否本人审批(防止跳节点审批)
        if (processDetail.getProcessDetailObjectType() == ASSET_OBJECT_TYPE_PERSON) {
            if (!processDetail.getProcessDetailObjectId().equals(userId)) {
                return CommonResult.failed(CommonCodeEnum.ASSET_ALLOW_FAIL);
            }
        } else {
            List<String> roleList = cacheUtil.getRoleList(userId);
            List<String> filterRoleList = roleList.stream().filter(role -> role.equals(processDetail.getProcessDetailObjectId())).collect(Collectors.toList());
            if (filterRoleList.size() < 1) {
                return CommonResult.failed(CommonCodeEnum.ASSET_ALLOW_FAIL);
            }
        }

        //校验上一个节点状态是否为“通过”
        if (!processDetail.getProcessDetailSort().equals(1)) {
            SmsAssetConsumableBorrowApplyProcessDetail preDetail =
                    smsAssetConsumableBorrowApplyProcessDetailMapper.selectOne(new LambdaQueryWrapper<SmsAssetConsumableBorrowApplyProcessDetail>()
                            .eq(SmsAssetConsumableBorrowApplyProcessDetail::getApplyId, allowReq.getApplyId())
                            .eq(SmsAssetConsumableBorrowApplyProcessDetail::getProcessDetailSort, processDetail.getProcessDetailSort() - 1));
            if (preDetail != null && isNullOrEmpty(preDetail.getId())) {
                if (preDetail.getProcessDetailStatus() != ASSET_ALLOW_STATUS_PASS) {
                    return CommonResult.failed(CommonCodeEnum.ASSET_ALLOW_FAIL);
                }
            }
        }

        SmsAssetConsumableBorrowApplyProcessDetail allowDetail = new SmsAssetConsumableBorrowApplyProcessDetail();
        allowDetail.setId(allowReq.getDetailId());
        allowDetail.setProcessDetailStatus(allowReq.getProcessDetailStatus());
        if (!isNullOrEmpty(allowReq.getProcessDetailRemark())) {
            allowDetail.setProcessDetailRemark(allowReq.getProcessDetailRemark());
        }

        Date date = new Date();
        allowDetail.setEndTime(date);
        allowDetail.setAllowUserId(userId);
        if (processDetail.getProcessDetailIsSign() == ASSET_BORROW_BOOLEAN_TRUE) {
            if (isNullOrEmpty(allowReq.getProcessDetailSign())) {
                ExceptionCast.cast(CommonResult.failed(CommonCodeEnum.INVALID_PARAM));
            }
            allowDetail.setProcessDetailSign(allowReq.getProcessDetailSign());
        }
        if (processDetail.getProcessDetailIsStamp() == ASSET_BORROW_BOOLEAN_TRUE) {
            if (isNullOrEmpty(allowReq.getProcessDetailStamp())) {
                ExceptionCast.cast(CommonResult.failed(CommonCodeEnum.INVALID_PARAM));
            }
            allowDetail.setProcessDetailStamp(allowReq.getProcessDetailStamp());
        }
        smsAssetConsumableBorrowApplyProcessDetailMapper.updateById(allowDetail);

        boolean is_done = false;

        //判断是否最后一个节点 是否驳回
        Integer detailCount = smsAssetConsumableBorrowApplyProcessDetailMapper.selectCount(new QueryWrapper<SmsAssetConsumableBorrowApplyProcessDetail>()
                .lambda()
                .eq(SmsAssetConsumableBorrowApplyProcessDetail::getApplyId, allowReq.getApplyId()));
        if (detailCount.equals(processDetail.getProcessDetailSort()) || allowReq.getProcessDetailStatus() == ASSET_BORROW_ALLOW_STATUS_DENY) {
            is_done = true;

            SmsAssetConsumableBorrowApply updateApply = new SmsAssetConsumableBorrowApply();
            updateApply.setId(allowReq.getApplyId());
            updateApply.setEndTime(date);
            updateApply.setAllowStatus(allowReq.getProcessDetailStatus());
            smsAssetConsumableBorrowApplyMapper.updateById(updateApply);
        }

//        //流程结束并且同意，从资产库删除对应的资产
//        //流程最后一个人审批完成并且审批通过
//        if (detailCount.equals(processDetail.getProcessDetailSort()) && allowReq.getProcessDetailStatus() == ASSET_BORROW_ALLOW_STATUS_PASS) {
//
//            List<SmsAssetConsumableBorrowApplyGoods> goodsList =
//                    smsAssetConsumableBorrowApplyGoodsMapper.selectList(new QueryWrapper<SmsAssetConsumableBorrowApplyGoods>()
//                            .lambda()
//                            .eq(SmsAssetConsumableBorrowApplyGoods::getApplyId, allowReq.getApplyId()));
//            if (goodsList != null && goodsList.size() > 0) {
//                for (SmsAssetConsumableBorrowApplyGoods goods : goodsList) {
//                    SmsAssetConsumableStorage storage = smsAssetConsumableStorageMapper.selectOne(new LambdaQueryWrapper<SmsAssetConsumableStorage>()
//                            .eq(SmsAssetConsumableStorage::getConsumableName, goods.getAssetName())
//                            .eq(SmsAssetConsumableStorage::getConsumableCategory, goods.getAssetCategory())
//                            .eq(SmsAssetConsumableStorage::getConsumableModel, goods.getAssetModel())
//                            .eq(SmsAssetConsumableStorage::getConsumablePrice, goods.getAssetPrice()));
//
//                    //校验库存
//                    if (storage.getConsumableNum() < goods.getAssetNum()) {
//                        ExceptionCast.cast(CommonResult.failed(CommonCodeEnum.CONSUMABLE_STORAGE_NOT_ENOUGH));
//                    }
//
//                    SmsAssetConsumableStorage smsAssetConsumableStorage = new SmsAssetConsumableStorage();
//                    smsAssetConsumableStorage.setId(storage.getId());
//                    //设置库存,设置总价
//                    if (storage.getConsumableNum().equals(goods.getAssetNum())) {
//                        smsAssetConsumableStorage.setConsumableNum(0);
//                        smsAssetConsumableStorage.setTotalPrice(BigDecimal.valueOf(0));
//                    } else {
//                        smsAssetConsumableStorage.setConsumableNum(storage.getConsumableNum() - goods.getAssetNum());
//                        Integer consumableNum = storage.getConsumableNum();
//                        BigDecimal totalPrice = storage.getConsumablePrice().multiply(BigDecimal.valueOf(consumableNum));
//                        smsAssetConsumableStorage.setTotalPrice(totalPrice);
//                    }
//                    smsAssetConsumableStorageMapper.updateById(smsAssetConsumableStorage);
//                }
//            }
//        }
        //节点驳回 重置其余节点状态
        if (allowReq.getProcessDetailStatus() == ASSET_BORROW_ALLOW_STATUS_DENY) {
            SmsAssetConsumableBorrowApplyProcessDetail updateDenyDetail =
                    new SmsAssetConsumableBorrowApplyProcessDetail();
            updateDenyDetail.setProcessDetailStatus(ASSET_BORROW_ALLOW_STATUS_DENY_FINISH);
            smsAssetConsumableBorrowApplyProcessDetailMapper.update(updateDenyDetail, new QueryWrapper<SmsAssetConsumableBorrowApplyProcessDetail>()
                    .lambda()
                    .eq(SmsAssetConsumableBorrowApplyProcessDetail::getApplyId, allowReq.getApplyId())
                    .eq(SmsAssetConsumableBorrowApplyProcessDetail::getProcessDetailStatus, ASSET_BORROW_ALLOW_STATUS_WAIT_ALLOW));
        }

        //消息通知
        if (is_done) {
            boolean result_status = false;
            if (allowReq.getProcessDetailStatus() == ASSET_BORROW_ALLOW_STATUS_PASS) {
                result_status = true;
            }
            newTipsUtil.allowFinishTips(allowReq.getApplyId(), userId, result_status);
        } else {
            SmsAssetConsumableBorrowApplyProcessDetail nextDetail =
                    smsAssetConsumableBorrowApplyProcessDetailMapper.selectOne(new QueryWrapper<SmsAssetConsumableBorrowApplyProcessDetail>()
                            .lambda()
                            .eq(SmsAssetConsumableBorrowApplyProcessDetail::getApplyId, allowReq.getApplyId())
                            .eq(SmsAssetConsumableBorrowApplyProcessDetail::getProcessDetailSort, processDetail.getProcessDetailSort() + 1));
            RangeTypeEnum rangeTypeEnum;
            if (nextDetail.getProcessDetailIsApplicantEdit().equals(1)) {
                rangeTypeEnum = RangeTypeEnum.RANGE_TYPE_ENUM_PERSON;
            } else {
                if (nextDetail.getProcessDetailObjectType() == ASSET_BORROW_OBJECT_TYPE_ROLE) {
                    rangeTypeEnum = RangeTypeEnum.RANGE_TYPE_ENUM_ROLE;
                } else {
                    rangeTypeEnum = RangeTypeEnum.RANGE_TYPE_ENUM_PERSON;
                }
            }

            List<String> rangeIDList = new ArrayList<>();
            rangeIDList.add(nextDetail.getProcessDetailObjectId());
            newTipsUtil.allowContinueTips(allowReq.getApplyId(),
                    userId,
                    rangeTypeEnum,
                    rangeIDList,
                    nextDetail.getId());
        }
        return CommonResult.success();
    }

    @Override
    public ResponseResult getAssetConsumableBorrowApplyList(Integer pageSize, Integer pageNum, Integer applyType, String userId) {
        if (isNullOrEmpty(applyType)) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        List<SmsAssetConsumableBorrowApplyResp> respList = new ArrayList<>();
        int total = 0;
        //查看已发起
        if (applyType == ASSET_BORROW_APPLY_TYPE_APPLY) {//已发起
            Page<SmsAssetConsumableBorrowApply> page = new Page<>(pageNum, pageSize);
            QueryWrapper<SmsAssetConsumableBorrowApply> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(SmsAssetConsumableBorrowApply::getApplyUserId, userId);
            smsAssetConsumableBorrowApplyMapper.selectPage(page, queryWrapper);
            respList = page.getRecords()
                    .stream()
                    .map(apply -> {
                        SmsAssetConsumableBorrowApplyResp borrowApplyResp =
                                new SmsAssetConsumableBorrowApplyResp();
                        BeanUtils.copyProperties(apply, borrowApplyResp);
                        borrowApplyResp.setApplyId(apply.getId());
                        SmsUser smsUser = cacheUtil.getUserInfo(apply.getApplyUserId());
                        SmsSearchUserResp userResp = new SmsSearchUserResp();
                        if (!isNullOrEmpty(smsUser)) {
                            BeanUtils.copyProperties(smsUser, userResp);
                        }
                        borrowApplyResp.setApplyUserInfo(userResp);
                        return borrowApplyResp;
                    })
                    .collect(Collectors.toList());
        } else if (applyType == ASSET_BORROW_APPLY_TYPE_ALLOWED) {//已办
            Page<SmsAssetConsumableBorrowApplyMPJResp> page = new Page<>(pageNum, pageSize);
            MPJLambdaWrapper<SmsAssetConsumableBorrowApplyProcessDetail> mpjLambdaWrapper = new MPJLambdaWrapper<>();
            mpjLambdaWrapper.selectAll(SmsAssetConsumableBorrowApplyProcessDetail.class)
                    .selectAll(SmsAssetConsumableBorrowApply.class)
                    .selectAs(SmsAssetConsumableBorrowApply::getCreateTime, "applyCreateTime")
                    .selectAs(SmsAssetConsumableBorrowApplyProcessDetail::getId, "detailId")
                    .leftJoin(SmsAssetConsumableBorrowApply.class, SmsAssetConsumableBorrowApply::getId, SmsAssetConsumableBorrowApplyProcessDetail::getApplyId)
                    .eq(SmsAssetConsumableBorrowApplyProcessDetail::getAllowUserId, userId)
                    //.orderByDesc(SmsAssetConsumableBorrowApply::getCreateTime)
                    .orderByDesc(SmsAssetConsumableBorrowApplyProcessDetail::getEndTime);
            smsAssetConsumableBorrowApplyProcessDetailMPJMapper.selectJoinPage(page, SmsAssetConsumableBorrowApplyMPJResp.class, mpjLambdaWrapper);
            respList = page.getRecords()
                    .stream()
                    .map(smsMPJResp -> {
                        SmsAssetConsumableBorrowApplyResp applyResp = new SmsAssetConsumableBorrowApplyResp();
                        BeanUtils.copyProperties(smsMPJResp, applyResp);
                        applyResp.setCreateTime(smsMPJResp.getApplyCreateTime());
                        applyResp.setEndTime(smsMPJResp.getEndTime());
                        applyResp.setApplyId(smsMPJResp.getApplyId());

                        SmsUser applyUser = cacheUtil.getUserInfo(applyResp.getApplyUserId());
                        SmsSearchUserResp applyUserResp = new SmsSearchUserResp();
                        if (applyUser != null) {
                            BeanUtils.copyProperties(applyUser, applyUserResp);
                        }
                        applyResp.setApplyUserInfo(applyUserResp);

                        SmsUser allowUser = cacheUtil.getUserInfo(applyResp.getAllowUserId());
                        SmsSearchUserResp allowUserResp = new SmsSearchUserResp();
                        if (allowUser != null) {
                            BeanUtils.copyProperties(allowUser, allowUserResp);
                        }
                        applyResp.setAllowUserInfo(allowUserResp);

                        return applyResp;
                    }).collect(Collectors.toList());
            total = (int) page.getTotal();
        } else if (applyType == ASSET_BORROW_APPLY_TYPE_ALLOW_WAIT_ME) {//待办
            List<String> roleList = cacheUtil.getRoleList(userId);
            //获取所有待审批申请流程节点
            List<SmsAssetConsumableBorrowApplyProcessDetail> allDetailList =
                    smsAssetConsumableBorrowApplyProcessDetailMapper.selectList(new QueryWrapper<SmsAssetConsumableBorrowApplyProcessDetail>()
                            .lambda()
                            .eq(SmsAssetConsumableBorrowApplyProcessDetail::getProcessDetailStatus, ASSET_BORROW_ALLOW_STATUS_WAIT_ALLOW));

            List<SmsAssetConsumableBorrowApplyProcessDetail> filterDetailList = allDetailList.stream()
                    .filter(detail -> {
                        if (detail.getProcessDetailIsApplicantEdit().equals(ASSET_BOOLEAN_TRUE)) {
                            if (!detail.getProcessDetailObjectId().equals(userId)) {
                                return false;
                            }
                        } else {
                            if (detail.getProcessDetailObjectType() == ASSET_BORROW_OBJECT_TYPE_PERSON) {
                                if (!detail.getProcessDetailObjectId().equals(userId)) {
                                    return false;
                                }
                            } else {
                                List<String> filterRoleList = roleList.stream().filter(role -> role.equals(detail.getProcessDetailObjectId())).collect(Collectors.toList());
                                if (filterRoleList.size() < 1) {
                                    return false;
                                }
                            }
                        }


                        if (detail.getProcessDetailSort().equals(1)) {
                            return true;
                        }
                        //判断上一个流程节点是否完成
                        SmsAssetConsumableBorrowApplyProcessDetail preDetail =
                                smsAssetConsumableBorrowApplyProcessDetailMapper.selectOne(new QueryWrapper<SmsAssetConsumableBorrowApplyProcessDetail>()
                                        .lambda()
                                        .eq(SmsAssetConsumableBorrowApplyProcessDetail::getApplyId, detail.getApplyId())
                                        .eq(SmsAssetConsumableBorrowApplyProcessDetail::getProcessDetailSort, detail.getProcessDetailSort() - 1)
                                        .eq(SmsAssetConsumableBorrowApplyProcessDetail::getProcessDetailStatus, ASSET_BORROW_ALLOW_STATUS_PASS));
                        if (preDetail == null || isNullOrEmpty(preDetail.getId())) {
                            return false;
                        }
                        return true;
                    })
                    .collect(Collectors.toList());
            if (filterDetailList.size() > 0) {
                List<String> detailIDList = filterDetailList.stream()
                        .map(SmsAssetConsumableBorrowApplyProcessDetail::getId)
                        .collect(Collectors.toList());

                Page<SmsAssetConsumableBorrowApplyMPJResp> page = new Page<>(pageNum, pageSize);
                MPJLambdaWrapper<SmsAssetConsumableBorrowApplyProcessDetail> mpjLambdaWrapper = new MPJLambdaWrapper<>();
                mpjLambdaWrapper.selectAll(SmsAssetConsumableBorrowApplyProcessDetail.class)
                        .selectAll(SmsAssetConsumableBorrowApply.class)
                        .selectAs(SmsAssetConsumableBorrowApply::getCreateTime, "applyCreateTime")
                        .selectAs(SmsAssetConsumableBorrowApplyProcessDetail::getId, "detailId")
                        .leftJoin(SmsAssetConsumableBorrowApply.class, SmsAssetConsumableBorrowApply::getId, SmsAssetConsumableBorrowApplyProcessDetail::getApplyId)
                        .in(SmsAssetConsumableBorrowApplyProcessDetail::getId, detailIDList)
                        .orderByDesc(SmsAssetConsumableBorrowApply::getCreateTime)
                        .orderByDesc(SmsAssetConsumableBorrowApplyProcessDetail::getCreateTime)
                ;
                smsAssetConsumableBorrowApplyProcessDetailMPJMapper.selectJoinPage(page, SmsAssetConsumableBorrowApplyMPJResp.class, mpjLambdaWrapper);

                respList = page.getRecords()
                        .stream()
                        .map(smsApplyMPJResp -> {
                            SmsAssetConsumableBorrowApplyResp applyResp = new SmsAssetConsumableBorrowApplyResp();
                            BeanUtils.copyProperties(smsApplyMPJResp, applyResp);
                            applyResp.setCreateTime(smsApplyMPJResp.getApplyCreateTime());
                            applyResp.setApplyId(smsApplyMPJResp.getApplyId());

                            SmsUser applyUser = cacheUtil.getUserInfo(smsApplyMPJResp.getApplyUserId());
                            SmsSearchUserResp smsExamineUserResp = new SmsSearchUserResp();
                            if (applyUser != null) {
                                BeanUtils.copyProperties(applyUser, smsExamineUserResp);
                            }
                            applyResp.setApplyUserInfo(smsExamineUserResp);

                            return applyResp;
                        })
                        .collect(Collectors.toList());
                total = (int) page.getTotal();
            }
        }

        return CommonResult.success(respList, total);
    }

    @Override
    public ResponseResult getAssetConsumableBorrowApplyDetail(String applyId, String userId) {
        if (isNullOrEmpty(applyId)) {
            ExceptionCast.cast(CommonResult.failed(CommonCodeEnum.INVALID_PARAM));
        }
        SmsAssetConsumableBorrowApply assetApply = smsAssetConsumableBorrowApplyMapper.selectById(applyId);
        if (assetApply == null || isNullOrEmpty(assetApply.getId())) {
            ExceptionCast.cast(CommonResult.failed(CommonCodeEnum.ASSET_BORROW_APPLY_NOT_EXIST));
        }
        SmsAssetConsumableBorrowApplyResp applyResp = new SmsAssetConsumableBorrowApplyResp();
        BeanUtils.copyProperties(assetApply, applyResp);
        applyResp.setApplyId(assetApply.getId());
        SmsUser smsUser = cacheUtil.getUserInfo(applyResp.getApplyUserId());
        SmsSearchUserResp smsUserResp = new SmsSearchUserResp();
        if (smsUser != null) {
            BeanUtils.copyProperties(smsUser, smsUserResp);
        }
        applyResp.setApplyUserInfo(smsUserResp);

        //封装流程节点
        List<SmsAssetConsumableBorrowApplyProcessDetailResp> detailList =
                smsAssetConsumableBorrowApplyProcessDetailMapper.selectList(new QueryWrapper<SmsAssetConsumableBorrowApplyProcessDetail>()
                                .lambda()
                                .eq(SmsAssetConsumableBorrowApplyProcessDetail::getApplyId, applyId)
                                .orderByAsc(SmsAssetConsumableBorrowApplyProcessDetail::getProcessDetailSort))
                        .stream()
                        .map(detail -> {
                            SmsAssetConsumableBorrowApplyProcessDetailResp detailResp = new SmsAssetConsumableBorrowApplyProcessDetailResp();
                            BeanUtils.copyProperties(detail, detailResp);
                            if (!isNullOrEmpty(detailResp.getAllowUserId())) {
                                SmsUser userInfo = cacheUtil.getUserInfo(detailResp.getAllowUserId());
                                SmsSearchUserResp smsSearchUserResp = new SmsSearchUserResp();
                                if (userInfo != null) {
                                    BeanUtils.copyProperties(userInfo, smsSearchUserResp);
                                }
                                detailResp.setAllowUserInfo(smsSearchUserResp);
                            }
                            if(detail.getProcessDetailObjectType() == PROCESS_OBJECT_TYPE_PERSON){
                                SmsUser allowUser = cacheUtil.getUserInfo(detail.getProcessDetailObjectId());
                                if(allowUser != null) {
                                    detailResp.setObjectName(allowUser.getRealName());
                                    detailResp.setObjectPic(allowUser.getUserPic());
                                }
                            }else {
                                SmsRole smsRole = smsRoleMapper.selectById(detail.getProcessDetailObjectId());
                                if(smsRole != null) {
                                    detailResp.setObjectName(smsRole.getRoleName());
                                }
                            }
                            return detailResp;
                        })
                        .collect(Collectors.toList());
        applyResp.setProcessList(detailList);
        //封装物品信息
        List<SmsAssetConsumableBorrowApplyGoodsResp> goodsList =
                smsAssetConsumableBorrowApplyGoodsMapper.selectList(new QueryWrapper<SmsAssetConsumableBorrowApplyGoods>()
                                .lambda()
                                .eq(SmsAssetConsumableBorrowApplyGoods::getApplyId, applyId))
                        .stream()
                        .map(goods -> {
                            SmsAssetConsumableBorrowApplyGoodsResp goodsResp = new SmsAssetConsumableBorrowApplyGoodsResp();
                            BeanUtils.copyProperties(goods, goodsResp);

                            SmsAssetConsumableCategoryManagement categoryManagement = smsAssetCategoryManagementMapper.selectById(goods.getAssetCategory());
                            if (categoryManagement != null) {
                                goodsResp.setAssetCategoryName(categoryManagement.getCategoryName());
                            }
                            return goodsResp;
                        })
                        .collect(Collectors.toList());
        applyResp.setGoodsList(goodsList);
        return CommonResult.success(applyResp);
    }

    @Override
    public ResponseResult selectAllAssetConsumableBorrowApply(Integer allowStatus, String applyPerson, String searchStr, String applyDepartment, String applyName, Integer pageSize, Integer pageNum) {

        LambdaQueryWrapper<SmsAssetConsumableBorrowApply> queryWrapper = new LambdaQueryWrapper<SmsAssetConsumableBorrowApply>()
                .orderByDesc(SmsAssetConsumableBorrowApply::getCreateTime);

        if (!isNullOrEmpty(applyPerson)) {
            queryWrapper.like(SmsAssetConsumableBorrowApply::getApplyPerson, applyPerson);
        }
        if (!isNullOrEmpty(allowStatus)) {
            queryWrapper.eq(SmsAssetConsumableBorrowApply::getAllowStatus, allowStatus);
        }
        if (!isNullOrEmpty(applyDepartment)) {
            queryWrapper.like(SmsAssetConsumableBorrowApply::getApplyDepartment, applyDepartment);
        }
        if (!isNullOrEmpty(applyName)) {
            List<String> userIdList = smsUserMapper.selectList(new LambdaQueryWrapper<SmsUser>()
                            .and(i -> {
                                i.like(SmsUser::getUsername, applyName)
                                        .or().like(SmsUser::getRealName, applyName);
                            }))
                    .stream()
                    .map(SmsUser::getId)
                    .collect(Collectors.toList());
            if (!userIdList.isEmpty()) {
                queryWrapper.in(SmsAssetConsumableBorrowApply::getApplyUserId, userIdList);
            }else {
                queryWrapper.eq(SmsAssetConsumableBorrowApply::getApplyUserId, "");
            }
        }
        if (!isNullOrEmpty(searchStr)) {
            List<String> applyList = smsAssetConsumableBorrowApplyGoodsMapper.selectList(new LambdaQueryWrapper<SmsAssetConsumableBorrowApplyGoods>()
                            .and(i -> {
                                i.like(SmsAssetConsumableBorrowApplyGoods::getAssetName, searchStr)
                                        .or().like(SmsAssetConsumableBorrowApplyGoods::getAssetModel, searchStr);
                            }))
                    .stream()
                    .map(SmsAssetConsumableBorrowApplyGoods::getApplyId)
                    .collect(Collectors.toList());
            if (!applyList.isEmpty()) {
                queryWrapper.in(SmsAssetConsumableBorrowApply::getId, applyList);
            }else {
                queryWrapper.eq(SmsAssetConsumableBorrowApply::getId, "");
            }
        }

        Page<SmsAssetConsumableBorrowApply> page = new Page<>(pageNum, pageSize);
        smsAssetConsumableBorrowApplyMapper.selectPage(page, queryWrapper);
        List<SmsAssetConsumableBorrowApplyResp> list  = page.getRecords()
                .stream()
                .map(smsAssetConsumableBorrowApply -> {
                    SmsAssetConsumableBorrowApplyResp borrowApplyResp =
                            new SmsAssetConsumableBorrowApplyResp();
                    BeanUtils.copyProperties(smsAssetConsumableBorrowApply, borrowApplyResp);
                    borrowApplyResp.setApplyId(smsAssetConsumableBorrowApply.getId());
                    SmsUser smsUser = cacheUtil.getUserInfo(smsAssetConsumableBorrowApply.getApplyUserId());
                    SmsSearchUserResp userResp = new SmsSearchUserResp();
                    if (!isNullOrEmpty(smsUser)) {
                        BeanUtils.copyProperties(smsUser, userResp);
                    }
                    borrowApplyResp.setApplyUserInfo(userResp);
                    return borrowApplyResp;
                })
                .collect(Collectors.toList());
        int total = (int) page.getTotal();
        return CommonResult.success(list, total);
    }

    @Override
    public ResponseResult selectDetail(String applyId) {
        QueryWrapper<SmsAssetConsumableBorrowApplyGoods> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SmsAssetConsumableBorrowApplyGoods::getApplyId, applyId);
        SmsAssetConsumableBorrowApplyGoods detail = smsAssetConsumableBorrowApplyGoodsMapper.selectOne(queryWrapper);
        if (isNullOrEmpty(detail)) {
            return CommonResult.failed(CommonCodeEnum.DATA_NOT_FOUND);
        }

        SmsAssetConsumableBorrowApplyGoodsResp smsAssetConsumableBorrowApplyGoodsResp = new SmsAssetConsumableBorrowApplyGoodsResp();
        BeanUtils.copyProperties(detail, smsAssetConsumableBorrowApplyGoodsResp);

        return CommonResult.success(smsAssetConsumableBorrowApplyGoodsResp);
    }
}
